package com.wuwu.springcloud.Controller;


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.wuwu.springcloud.pojo.Dept;
import com.wuwu.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

// 提供Restful 服务
@RestController
public class DeptController {
    @Autowired
     DeptService deptService;
    //获取一些配置信息 得到具体的微服务
    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/dept/get/{id}")
    @HystrixCommand(fallbackMethod = "HystrixGet")
    public Dept get(@PathVariable("id") Long id){
        Dept dept = deptService.queryBuId(id);
        if(dept == null){
             throw new RuntimeException(id+"不存在，请检查");
        }
        return dept;
    }
   //备选方案
    public Dept HystrixGet(@PathVariable("id") Long id){

        return new Dept()
                .setDeptno(id)
                .setDname("没有对应信息 null")
                .setDb_source("no this database in MySql");
    }



//
//    @PostMapping("/dept/add")
//     public boolean add(Dept dept){
//          return deptService.addDept(dept);
//     }
//
//
//     @GetMapping("/dept/list")
//    public List<Dept> queryAll(){
//        return deptService.queryList();
//    }
//
//    @GetMapping("/dept/discovery")
//    public Object discovery(){
//        // 获取 所有微服务清单
//         List<String> Service = discoveryClient.getServices();
//         System.out.println("discovery------"+Service);
//         //得到具体的一个微服务信息    id
//         List<ServiceInstance> instances = discoveryClient.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
//         for (ServiceInstance instance : instances) {
//             //host 主机名称  port  端口号  url 地址
//             System.out.println(
//                     instance.getHost()+"\t"+
//                     instance.getPort()+"\t"+
//                     instance.getUri()+"\t"+
//                     instance.getServiceId()+"\t"
//
//             );
//         }
//         return this.discoveryClient;
//     }
}
